package cn.bellychang.deferredresult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.management.*;
import java.util.Set;

/**
 * @author ChangLiang
 * @date 2020/2/21
 */
@RestController
public class HelloWorldController {

    @Autowired
    private MBeanServer mbeanServer;

    @GetMapping("/hello")
    public String index() {
        try {
            Set<ObjectName> objectNames = mbeanServer.queryNames(new ObjectName("Tomcat:type=ThreadPool,*"), null);
            if (objectNames != null && objectNames.size() == 1) {
                for (ObjectName objectName : objectNames) {
                    int maxThreads = (Integer) mbeanServer.getAttribute(objectName, "maxThreads");
                    int currentThreadCount = (Integer) mbeanServer.getAttribute(objectName, "currentThreadCount");
                    int currentThreadsBusy = (Integer) mbeanServer.getAttribute(objectName, "currentThreadsBusy");
                    long connectionCount = (Long) mbeanServer.getAttribute(objectName, "connectionCount");
                    int maxConnections = (Integer) mbeanServer.getAttribute(objectName, "maxConnections");
                }
            } else {

            }
        } catch (MalformedObjectNameException | MBeanException | AttributeNotFoundException | InstanceNotFoundException | ReflectionException e) {
            e.printStackTrace();
        }
        return "hello world";
    }
}
